package PlugIn.commands;

import java.io.File;
import java.util.Collection;

import FilePanel.Data;
import PlugIn.PlugInModel;

public class Size extends PlugInModel{
	
	/**
	 * constructor
	 * @param data
	 */
	public Size(Data data) {
		super(data);
	}

	private String result;
	
	/**
	 * returns the result
	 */
	public String getResult() {
		return result;
	}

	/**
	 * sets the result
	 * @param result
	 */
	private void setResult(String result) {
		this.result = result;
	}
	/**
	 * manages when we are dealing with files
	 */
	protected void manageFile(File f) {
		this.result=String.valueOf(f.length());
	}
	/**
	 * manages when we are dealing with directories
	 */
	protected void manageDir(File f) {
		long size=0;
		File[] listFiles=f.listFiles();
		for (File file : listFiles) {
			if (file.isDirectory()) {
				manageDir(file);
				size+=Long.parseLong(getResult());
			}
			else 
				size+=file.length();
		}
		
		setResult(String.valueOf(size));
	}
	/**
	 * manages when we are dealing when we have nothing
	 */
	protected void manageEmpty() {
		setResult("0");
	}

	/**
	 * manages when we are dealing with multiple directorie or files
	 */
	protected void manageMultiple(Collection<File> lf) {
		long size=0;
		for (File f : lf) {
			if(f.isDirectory()){
				manageDir(f);
			}
			else{
				manageFile(f);
			}
			size+=Long.parseLong(getResult());
		}
		setResult(String.valueOf(size));
		
	}

	@Override
	/**
	 * gets the name
	 * return String "Taille"
	 */
	public String getName() {
		return "Taille";
	}
		
	
}

